package cn.edu.neusoft.yuejian_02.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.edu.neusoft.yuejian_02.R;
import cn.edu.neusoft.yuejian_02.bean.UserBean;
import cn.edu.neusoft.yuejian_02.listener.RetrofitListener;
import cn.edu.neusoft.yuejian_02.model.UserModel;
import es.dmoral.toasty.Toasty;

public class RegActivity extends AppCompatActivity implements RetrofitListener{

    private EditText username,password,nickname,age;
    private Button regbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        View bg = findViewById(R.id.activity_reg);
        bg.getBackground().setAlpha(130);
        View btln = findViewById(R.id.button);
        btln.getBackground().setAlpha(150);
        initView();

        initEvent();
    }



    private void initView() {
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        nickname = (EditText) findViewById(R.id.nickname);
        age = (EditText) findViewById(R.id.age);
        regbtn = (Button) findViewById(R.id.button);

    }




    private void initEvent() {
        regbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel usermodel = new UserModel();

                usermodel.Reg(username.getText().toString(), password.getText().toString(),
                        nickname.getText().toString(),age.getText().toString(),RegActivity.this);


            }
        });

    }

    @Override
    public void onSuccess(Object object) {
        UserBean userBean = (UserBean)object;
        String uid =String.valueOf( userBean.getUser_id());
        if ("0".equals(uid)) {
            Toasty.error(RegActivity.this, "注册失败").show();
        } else {
            Toasty.success(RegActivity.this, "注册成功").show();
            Intent intent = new Intent(RegActivity.this,LoginActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onFail() {
        Toast.makeText(RegActivity.this, "失败", Toast.LENGTH_SHORT).show();
    }
}

